package com.humble.vichatbackend.web_socket;

import com.humble.vichatbackend.mapper.TeamMapper;
import com.humble.vichatbackend.mapper.UserTeamMapper;
import jakarta.annotation.PostConstruct;
import jakarta.websocket.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class ChatSocketManager {
    /**
     *  teamId:chatSocket(聊天室)
     */
    public static final Map<Long,ChatSocket> CHAT_SOCKET_MAP = new ConcurrentHashMap<>();
    
    public static final Map<Long, Session> USER_SESSION_MAP = new ConcurrentHashMap<>();

    TeamMapper teamMapper;
    UserTeamMapper userTeamMapper;
    @Autowired
    ChatSocketManager(TeamMapper teamMapper, UserTeamMapper userTeamMapper) {
        this.teamMapper = teamMapper;
        this.userTeamMapper = userTeamMapper;
    }

    //初始化各个聊天室及其成员；
    @PostConstruct
    public void init() {
        List<Long> teamIds = teamMapper.selectAllIds();
        for (Long teamId : teamIds) {
            ChatSocket chatSocket = new ChatSocket();
            List<Long> userIds = userTeamMapper.selectUserIdsByTeamId(teamId);
            Set<Long> teamUserid = chatSocket.getTEAM_USERID();
            teamUserid.addAll(userIds);
            CHAT_SOCKET_MAP.put(teamId,chatSocket);
        }
    }
}
